package lihua.robot.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lihua.robot.mapper.CrmWarehouseMapper;
import lihua.robot.pojo.CrmWarehouse;
import lihua.robot.service.CrmWarehouseService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huanghan
 * @description 针对表【crm_warehouse】的数据库操作Service实现
 * @createDate 2024-02-23 13:07:27
 */
@Service
public class CrmWarehouseServiceImpl extends ServiceImpl<CrmWarehouseMapper, CrmWarehouse>
        implements CrmWarehouseService {

    @Override
    @DS("slave_2")
    public Integer selectKwzt(String AP) {
        CrmWarehouse state =this.baseMapper.selectByAP(AP);
        return Integer.parseInt(state.getStatus());
    }

    @Override
    @DS("slave_2")
    public int queryEmptyBucketCapacityCount() {
        LambdaQueryWrapper<CrmWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmWarehouse::getBarrelType,2)
                .eq(CrmWarehouse::getStatus,1)
                .eq(CrmWarehouse::getWarehouseType,2);
        List<CrmWarehouse> crmWarehouse = getBaseMapper().selectList(wrapper);
        return Math.toIntExact(crmWarehouse.size());
    }

    @Override
    @DS("slave_2")
    public String queryXFullBucketCapacity(int i, Integer deSvn) {
        LambdaQueryWrapper<CrmWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmWarehouse::getBarrelType,3)
                .eq(CrmWarehouse::getStatus,1)
                .eq(CrmWarehouse::getWarehouseType,1)
                .eq(CrmWarehouse::getWarehouseNo,deSvn);
        CrmWarehouse crmWarehouse = getBaseMapper().selectOne(wrapper);
        if(crmWarehouse == null) {
            LambdaQueryWrapper<CrmWarehouse> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(CrmWarehouse::getBarrelType, 3)
                    .eq(CrmWarehouse::getStatus, 1)
                    .eq(CrmWarehouse::getWarehouseType, 2);
            List<CrmWarehouse> crmWarehouse1 = getBaseMapper().selectList(wrapper1);
            if (!crmWarehouse1.isEmpty()) {
                System.out.println(crmWarehouse1);
                return crmWarehouse1.get(i).getWarehouseName();
            }
            else return null;
        }
        return crmWarehouse.getWarehouseName();
    }

    @Override
    @DS("slave_2")
    public String queryEmptyBucketCapacity(int i, int deSvn) {
        LambdaQueryWrapper<CrmWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmWarehouse::getBarrelType,2)
                .eq(CrmWarehouse::getStatus,1)
                .eq(CrmWarehouse::getWarehouseType,1)
                .eq(CrmWarehouse::getWarehouseNo,deSvn);
        CrmWarehouse crmWarehouse = getBaseMapper().selectOne(wrapper);
        if(crmWarehouse == null){
            LambdaQueryWrapper<CrmWarehouse> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(CrmWarehouse::getBarrelType,2)
                    .eq(CrmWarehouse::getStatus,1)
                    .eq(CrmWarehouse::getWarehouseType,2);
            List<CrmWarehouse> crmWarehouse1 = getBaseMapper().selectList(wrapper1);
            if(!crmWarehouse1.isEmpty()){
                return crmWarehouse1.get(i).getWarehouseName();
            }
            return "暂无库位";
        }
        return crmWarehouse.getWarehouseName();
    }

    @Override
    @DS("slave_2")
    public String queryFullBucketCapacity(int i, int deSvn) {
        LambdaQueryWrapper<CrmWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmWarehouse::getBarrelType,1)
                .eq(CrmWarehouse::getStatus,0)
                .eq(CrmWarehouse::getWarehouseType,1)
                .eq(CrmWarehouse::getWarehouseNo,deSvn);
        CrmWarehouse crmWarehouse = getBaseMapper().selectOne(wrapper);
        if(crmWarehouse != null){
            return crmWarehouse.getWarehouseName();
        }
        else {
            LambdaQueryWrapper<CrmWarehouse> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(CrmWarehouse::getBarrelType,1)
                    .eq(CrmWarehouse::getStatus,0)
                    .eq(CrmWarehouse::getWarehouseType,2);
            List<CrmWarehouse> crmWarehouse1 =getBaseMapper().selectList(wrapper1);
            if(!crmWarehouse1.isEmpty()){
                System.out.println(crmWarehouse1);
                System.out.println("123--"+crmWarehouse1.get(i).getWarehouseName());
                return crmWarehouse1.get(i).getWarehouseName();
            }
            return "暂无库位";
        }
    }

    @Override
    @DS("slave_2")
    public Integer queryFullBucketCapacityCount() {
        LambdaQueryWrapper<CrmWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmWarehouse::getBarrelType,1)
                .eq(CrmWarehouse::getStatus,0)
                .eq(CrmWarehouse::getWarehouseType,2);
        List<CrmWarehouse> crmWarehouse = getBaseMapper().selectList(wrapper);
        return Math.toIntExact(crmWarehouse.size());
    }

    @Override
    @DS("slave_2")
    public Integer queryXFullBucketCapacityCount() {
        LambdaQueryWrapper<CrmWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmWarehouse::getBarrelType,3)
                .eq(CrmWarehouse::getStatus,1)
                .eq(CrmWarehouse::getWarehouseType,2);
        List<CrmWarehouse> crmWarehouse = getBaseMapper().selectList(wrapper);
        return Math.toIntExact(crmWarehouse.size());
    }

    @Override
    @DS("slave_2")
    public String queryXEmptyBucketCapacity(int i) {
        LambdaQueryWrapper<CrmWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmWarehouse::getBarrelType,4)
                .eq(CrmWarehouse::getStatus,0)
                .eq(CrmWarehouse::getWarehouseType,2);
        List<CrmWarehouse> crmWarehouse = getBaseMapper().selectList(wrapper);
        if(!crmWarehouse.isEmpty()){
            return crmWarehouse.get(i).getWarehouseName();
        }
        return null;
    }

    @Override
    @DS("slave_2")
    public Integer queryXEmptyBucketCapacityCount() {
        LambdaQueryWrapper<CrmWarehouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CrmWarehouse::getBarrelType,4)
                .eq(CrmWarehouse::getStatus,0)
                .eq(CrmWarehouse::getWarehouseType,2);
        List<CrmWarehouse> crmWarehouse = getBaseMapper().selectList(wrapper);
        return Math.toIntExact(crmWarehouse.size());
    }


}




